﻿namespace CatClient.Message.Spi.Internals
{
    using CatClient.Message.Spi;
    using System;
    using System.Threading;

    public class DefaultMessageStatistics : IMessageStatistics
    {
        private long _produced;
        private long _overflowed;
        private long _bytes;

        public void OnBytes(int size)
        {
            Interlocked.Add(ref this._bytes, (long) size);
        }

        public void OnOverflowed()
        {
            Interlocked.Increment(ref this._overflowed);
        }

        public void OnSending()
        {
            Interlocked.Increment(ref this._produced);
        }

        public void Reset()
        {
            Interlocked.Exchange(ref this._produced, 0L);
            Interlocked.Exchange(ref this._overflowed, 0L);
            Interlocked.Exchange(ref this._bytes, 0L);
        }

        public long Produced =>
            Interlocked.CompareExchange(ref this._produced, -1L, -1L);

        public long Overflowed =>
            Interlocked.CompareExchange(ref this._overflowed, -1L, -1L);

        public long Bytes =>
            Interlocked.CompareExchange(ref this._bytes, -1L, -1L);
    }
}

